package cn.jishiyu11.xeqb.activity.personalCenterActivitys

import android.os.Bundle
import android.view.View
import android.widget.TextView
import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.base.BaseActivity
import cn.jishiyu11.xeqb.http.http.i.DataCallBack
import cn.jishiyu11.xeqb.http.http.i.httpbase.*
import cn.jishiyu11.xeqb.utils.*
import cn.jishiyu11.xeqb.view.CustomDialog
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.act_realname.*
import kotlinx.android.synthetic.main.tab_activity_fra.*
import okhttp3.Request
import org.json.JSONObject
import java.io.IOException
import java.util.HashMap

/**
 * Created by jsy_zj on 2018-03-10.
 */
class RealNameActivity:BaseActivity(),DataCallBack {

    var customDialog:CustomDialog? =null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_realname)
        findViewById()
    }


    override fun findViewById() {
        title_image.visibility = View.VISIBLE
        title_image.setOnClickListener { finish() }
        (title_view as TextView).text = this.getString(R.string.name_act_realname_identity)

        title_image.setOnClickListener {
            finish()
        }
        act_realname_commit.setOnClickListener {
            //            this.showShortToast("成功!")
            //检验/上传信息
            checkInput()

        }
        customDialog = CustomDialogUtils.instance.LoadingDialog(this,"正在认证中...")
    }

    override fun initView() {
    }

    /**
     * 检查输入
     * */
    fun checkInput(){

        if (!act_realname_name.text.isNullOrEmpty()){

            if (!act_realname_cardid.text.isNullOrEmpty()){

                if (IdcardValidator.instance.isValidatedAllIdcard(act_realname_cardid.text.toString())) {
                    postData()

                }else{
                    this.showShortToast("非法身份证号！")
                }

            }else{
                this.showShortToast("身份证号不能为空！")
            }
        }else{
            this.showShortToast("姓名不能为空！")
        }

    }

    /**
     * 上传信息
     * */
    fun postData(){
        customDialog?.show()
        //实名认证
        var map = HashMap<String, Any>()
        map.put(getString(R.string.userName),act_realname_name.text.toString())
        map.put(getString(R.string.userIdCard),act_realname_cardid.text.toString())
        var postMap = HashMap<String, Any>()
        getPostMap(this, USER_IDCARD, map, postMap, SharedPreferencesUtils.get(this, getString(R.string.token), "").toString())
        OkHttpManager.postAsync(ANDROID_HTTP, "IDENTITY_REALNAME", postMap, this)
    }

    override fun requestFailure(request: Request, name: String, e: IOException) {
        when (name) {
            "IDENTITY_REALNAME"//登录返回值
            -> {this.showShortToast(this.getString(R.string.network_timed))
            customDialog?.dismiss()
            }
        }
    }

    override fun requestSuccess(result: String, name: String) {
        var objects:JSONObject? = null
        when(name){
            "IDENTITY_REALNAME" ->{
                customDialog?.dismiss()
//                println(result)
                objects = JSONObject(result)
                if (result.contains("reqData")) {
                    var data = objects.get("reqData").toString()
                    var key = SharedPreferencesUtils.get(this, getString(R.string.token), "").toString().substring(16, 32)
                    var keydCode = AESUtils.decrypt(key, data)//解密
                    objects = JSONObject(keydCode)
                    if (objects.get("code").equals("200")) {
                        //修改成功
                        this.showShortToast( "认证成功！")
                        SharedPreferencesUtils.put(this,getString(R.string.personalcenter_isrealname),"1")
                        finish()//关闭当前界面
                    } else {
                        this.showShortToast( objects.get("msg").toString())
                    }
                }

            }
        }
    }

    override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        KeyBoardUtils.closeKeyboard(this,act_realname_name)
        MobclickAgent.onPause(this)
    }



}